package cn.hutool.benchmark.json;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.engine.JSONEngine;
import org.dromara.hutool.json.engine.JSONEngineFactory;
import org.dromara.hutool.json.serializer.JSONSerializer;
import org.dromara.hutool.json.serializer.TypeAdapterManager;
import org.openjdk.jmh.annotations.*;

import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间，越少越好
@Warmup(iterations = 5, time = 1) //预热5次调用
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5次，每次1秒
@Threads(1) //单线程
@Fork(1) //
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位：纳秒
@State(Scope.Benchmark) // 共享域
public class JSONEngineToJsonStringJmh {

	private JSONEngine fastJSONEngine;
	private JSONEngine jacksonEngine;
	private JSONEngine gsonEngine;
	private JSONEngine hutoolJSONEngine;

	private TestBean testBean;

	@Setup
	public void setup(){
		fastJSONEngine = JSONEngineFactory.createEngine("fastjson");
		jacksonEngine = JSONEngineFactory.createEngine("jackson");
		gsonEngine = JSONEngineFactory.createEngine("gson");
		hutoolJSONEngine = JSONEngineFactory.createEngine("hutool");

		testBean = new TestBean();
		testBean.setName("AAAA");
		testBean.setAge(19);
		testBean.setGender(true);
	}

	@Benchmark
	public void fastJSONJmh(){
		fastJSONEngine.toJsonString(testBean);
	}

	@Benchmark
	public void jacksonJmh(){
		jacksonEngine.toJsonString(testBean);
	}

	@Benchmark
	public void gsonJmh(){
		gsonEngine.toJsonString(testBean);
	}

	@Benchmark
	public void hutoolJmh(){
		hutoolJSONEngine.toJsonString(testBean);
	}

	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	static class TestBean {
		private String name;
		private int age;
		private boolean gender;
	}
}
